package com.simpleframe.views.ui;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.simpleframe.views.components.Component;

public abstract class ComponentTagSupport extends BodyTagSupport{

	private static final long serialVersionUID = 1L;
	
	protected Component component;
	
	@Override
	public int doStartTag() throws JspException {
		component = getModelBean();
		populateParameters();
		boolean evalBody = component.start(pageContext.getOut());
		
        if (evalBody) {
            return component.usesBody() ? EVAL_BODY_BUFFERED : EVAL_BODY_INCLUDE;
        } else {
            return SKIP_BODY;
        }
	}
	
	@Override
	public int doEndTag() throws JspException {
		component.end(pageContext.getOut(), "");
		return EVAL_PAGE;
	}
	
	public Component getComponent(){
		return this.component;
	}
	
	public abstract Component getModelBean();
	
	protected void populateParameters(){
		
	}
}
